import { _decorator, Component, Node, ScrollView, UIRenderer, Mask, math, UITransform } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MergeItem')
export class MergeItem extends Component {

    levelChildren: Node[][] = [];

    uiTrans: UITransform;

    onLoad() {
        this.uiTrans = this.node.getComponent(UITransform);
        this.node.on("merge_item_node_changed", this.refreshChildNodeInfo, this);

        this.refreshChildNodeInfo();
        this.node["mergeItem"] = this
    }

    onEnable() {
        // this.refreshChildNodeInfo();
        // this.node["mergeItem"] = this
    }

    private refreshChildNodeInfo() {

        const levels = [[this.node]];       

        let level = 1;

        const dfs = function (node: Node) {
            levels[level] = levels[level] || [];
            levels[level].push(node);
            // if (node.getComponent(ScrollView)) { //如果是scrollview就不再往深层去
            //     node["hasScrollView"] = true;
            //     return;
            // }
            const comps = node.components || [];
            for (const comp of comps) {
                if (comp instanceof UIRenderer) {
                    node["hasUIRenderer"] = true;
                }
                if (comp instanceof Mask) {
                    node["hasMask"] = true;
                }
                if (comp instanceof ScrollView) {
                    node["hasScrollView"] = true;
                    return;
                }
            }
            if (node.children.length) {
                ++level;
                for (let child of node.children) {
                    dfs(child);
                }
                --level;
            }
        }

        for (let child of this.node.children) {
            dfs(child);
        }

        this.levelChildren = levels;

        function printChildren(levelNodes: Node[][]) {
            // for (let i = 0, len = levelNodes.length; i < len; i++) {
            //     let nodes = levelNodes[i];
            //     for (const node of nodes) {
            //         console.log(" level children ... level " + i + "    " + "name: ", node.name);
            //     }
            // }
        }

        printChildren(levels);

    }

    getBoundingBox(): math.Rect | null {
        return null;
    }

    // levelOrderTraversal(root: Node): Node[][] {
    //     const result: Node[][] = [];
    //     if (!root) return result;

    //     const queue: { node: Node,level: number }[] = [{ node: root, level: 0 }];

    //     while (queue.length > 0) {
    //         const { node, level } = queue.shift()!;

    //         // 确保结果数组中存在当前层级的子数组
    //         if (result.length <= level) {
    //             result.push([]);
    //         }

    //         result[level].push(node);

    //         // 将子节点加入队列，层级为当前层级+1
    //         for (const child of node.children) {
    //             queue.push({ node: child, level: level + 1 });
    //         }
    //     }

    //     return result;
    // }
}


